<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <div>
            <input type="text" v-model = 'nam'> 
            <button @click = "add">添加</button>
        </div>
        <ul>
            <!-- 选中吕不韦，在添加一个新的内容后，选中的是李斯。这就是没有key导致产生的问题 -->
            <!-- vue组件高度复用增加key可以标识组件的唯一性，为了更好的区别各个组件，key的作用主要是为了高效的更新虚拟DOM -->
            <li v-for = "(item, index) in list" :key = "item.id">
                <input type="checkbox">{{item.name}}
            </li>
        </ul>
    </div>
    <script>
        const vm = new Vue({
            el: '#app',
            data: {
                nam: '',
                newId: 3,
                list: [
                    {id: 1, name: "李斯"},
                    {id: 2, name: "吕不韦"},
                    {id: 3, name: "嬴政"}
                ]
            },
            methods: {
                add() {
                    this.list.unshift({id: ++this.newId, name: this.nam})
                    this.nam = ''
                }
            }
        })
    </script>
</body>
</html>